home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 23 / CU Amiga - Super CD-ROM 23 (June 1998).iso / CUCD / Magazine / C_Tutorial / Part-7 / arexx1 / idcmp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-11-26  |  6.2 KB  |  251 lines

  1. #include "idcmp.h"
  2. #include "drawwin.h"
  3. #include "gadgets.h"
  4. #include "loadsave.h"
  5. #include "menu.h"
  6. #include "toolwin.h"
  7. #include "arexx.h"
  8.  
  9. #include<string.h>
  10. #include<stdio.h>
  11.  
  12. #include<clib/exec_protos.h>
  13. #include<clib/gadtools_protos.h>
  14. #include<clib/graphics_protos.h>
  15. #include<clib/intuition_protos.h>
  16.  
  17. static void doGadgetUp(struct Window*, UWORD, struct Gadget*);
  18. static int  doMenuPick(struct Window*, UWORD);
  19. static int  doARexx(struct RexxMsg*, struct Window*);
  20.  
  21. /* Our message handling code */
  22. void handleIDCMP()
  23. {
  24.     char* text = "Hello World!";
  25.     int going = TRUE;
  26.     int drawing = FALSE;
  27.     ULONG drawsig, toolsig, arexxsig, gotsig;
  28.     struct Window* drawwin = getDrawWin();
  29.     drawsig = 1 << drawwin->UserPort->mp_SigBit;
  30.     arexxsig = getARexxSig();
  31.     while(going)
  32.     {
  33.         struct IntuiMessage* intuimsg;
  34.         /* Only include tool window signal mask if window is open */
  35.         toolsig = getToolSig();
  36.         /* Wait for messages to arrive */
  37.         gotsig = Wait(drawsig | toolsig | arexxsig);
  38.         /* Messages have arrived: loop through all of them */
  39.         /* Check messages from the drawing window first */
  40.         if(gotsig & drawsig)
  41.         {
  42.             while(intuimsg = GT_GetIMsg(drawwin->UserPort))
  43.             {
  44.                 /* Copy the important bits of the message */
  45.                 ULONG class = intuimsg->Class;
  46.                 UWORD code = intuimsg->Code;
  47.                 WORD mousex = intuimsg->MouseX;
  48.                 WORD mousey = intuimsg->MouseY;
  49.                 /* Reply when finished copying bits from message */
  50.                 GT_ReplyIMsg(intuimsg);
  51.                 /* Act on this message... */
  52.                 switch(class)
  53.                 {
  54.                 case IDCMP_MOUSEBUTTONS:
  55.                     switch(code)
  56.                     {
  57.                     case SELECTDOWN:
  58.                         drawing = TRUE;
  59.                         break;
  60.                     case SELECTUP:
  61.                         drawing = FALSE;
  62.                         break;
  63.                     }
  64.                     /* break; omitted so we draw on click, too */
  65.                 case IDCMP_MOUSEMOVE:
  66.                     if(drawing)
  67.                     {
  68.                         Move(drawwin->RPort, mousex, mousey);
  69.                         Text(drawwin->RPort, text, strlen(text));
  70.                     }
  71.                     break;
  72.                 case IDCMP_MENUPICK:
  73.                     going = doMenuPick(drawwin, code);
  74.                     drawwin = getDrawWin();
  75.                     drawsig = 1 << drawwin->UserPort->mp_SigBit;
  76.                     break;
  77.                 }
  78.             }
  79.         }
  80.         /* Now check messages from the tool window */
  81.         if(going && (gotsig & toolsig))
  82.         {
  83.             struct Window* toolwin = getToolWin();
  84.             while(toolwin && (intuimsg = GT_GetIMsg(toolwin->UserPort)))
  85.             {
  86.                 /* Copy the important bits of the message */
  87.                 ULONG class = intuimsg->Class;
  88.                 UWORD code = intuimsg->Code;
  89.                 APTR iaddr = intuimsg->IAddress;
  90.                 /* Reply when finished copying bits from message */
  91.                 GT_ReplyIMsg(intuimsg);
  92.                 /* Act on this message... */
  93.                 switch(class)
  94.                 {
  95.                 case IDCMP_CLOSEWINDOW:
  96.                     closeToolWin();
  97.                     /* Update our local toolwin, so we stop loop */
  98.                     toolwin = NULL;
  99.                     uncheckToolBar(drawwin);
  100.                     break;
  101.                 case IDCMP_REFRESHWINDOW:
  102.                     /* You *MUST* remember to ask for and handle these refresh messages */
  103.                     GT_BeginRefresh(toolwin);
  104.                     GT_EndRefresh(toolwin, TRUE);
  105.                     break;
  106.                 case IDCMP_GADGETUP:
  107.                     doGadgetUp(drawwin, code, (struct Gadget*)iaddr);
  108.                     break;
  109.                 case IDCMP_MENUPICK:
  110.                     going = doMenuPick(drawwin, code);
  111.                     /* Update our local toolwin, so we stop loop */
  112.                     toolwin = getToolWin();
  113.                     drawwin = getDrawWin();
  114.                     drawsig = 1 << drawwin->UserPort->mp_SigBit;
  115.                     break;
  116.                 }
  117.             }
  118.         }
  119.         /* Now check messages from the ARexx port */
  120.         if(going && (gotsig & arexxsig))
  121.         {
  122.             struct RexxMsg* msg;
  123.             while(going && (msg = getARexxMsg()))
  124.                 going = doARexx(msg, drawwin);
  125.         }
  126.     }
  127. }
  128.  
  129. /* Process IDCMP_GADGETUP event */
  130. static void doGadgetUp(struct Window* drawwin, UWORD code, struct Gadget* gad)
  131. {
  132.     switch(gad->GadgetID)
  133.     {
  134.     case MYBUT_ID:
  135.         /* Our button was clicked!  Set foreground to next pen colour */
  136.         nextFgPen(drawwin);
  137.         break;
  138.     case MYPAL_ID:
  139.         /* Our palette gadget was clicked!  Set foreground to gadget colour */
  140.         setFgPen(drawwin, code);
  141.         break;
  142.     }
  143. }
  144.  
  145. /* Process IDCMP_MENUPICK event */
  146. static int doMenuPick(struct Window* drawwin, UWORD code)
  147. {
  148.     UWORD menuCode, menuNumber, itemNumber;
  149.     /* Loop over all the menu selections in the menu code */
  150.     struct MenuItem* item;
  151.     for(menuCode = code;
  152.             menuCode != MENUNULL;
  153.             menuCode = item->NextSelect)
  154.     {
  155.         item = ItemAddress(drawwin->MenuStrip, menuCode);
  156.         /* Extract the menu number and menu item number from the menu code */
  157.         menuNumber = MENUNUM(menuCode);
  158.         itemNumber = ITEMNUM(menuCode);
  159.         /* Now decide what to do based on what menu item was selected */
  160.         switch(menuNumber)
  161.         {
  162.         case 0:  /* Project menu */
  163.             /* Only one item: Quit */
  164.             switch(itemNumber)
  165.             {
  166.             case 0:  /* Load */
  167.                 return load();
  168.             case 1:  /* Save */
  169.                 save();
  170.                 break;
  171.             case 3:  /* Quit (item 2 is the bar!) */
  172.                 return FALSE;
  173.             }
  174.             break;
  175.         case 1:  /* Pen menu */
  176.             switch(itemNumber)
  177.             {
  178.             case 0:  /* Next */
  179.                 nextFgPen(drawwin);
  180.                 break;
  181.             case 1:  /* Prev */
  182.                 prevFgPen(drawwin);
  183.                 break;
  184.             case 3:  /* Reset (item 2 is the bar!) */
  185.                 resetFgPen(drawwin);
  186.                 break;
  187.             }
  188.             break;
  189.         case 2:  /* Tools menu */
  190.             switch(itemNumber)
  191.             {
  192.             case 0:  /* Screen Bar */
  193.                 ShowTitle(drawwin->WScreen, item->Flags & CHECKED);
  194.                 break;
  195.             case 1:  /* Tool Bar */
  196.                 /* Do the open or close */
  197.                 if(item->Flags & CHECKED)
  198.                 {
  199.                     /* If the open fails, stop immediately */
  200.                     if(!openToolWin())
  201.                         return FALSE;
  202.                 }
  203.                 else
  204.                     closeToolWin();
  205.                 break;
  206.             }
  207.         }
  208.     }
  209.     /* Keep going */
  210.     return TRUE;
  211. }
  212.  
  213. /* Process an ARexx message */
  214. static int doARexx(struct RexxMsg* msg, struct Window* drawwin)
  215. {
  216.     int going = TRUE;
  217.     /* By default, our reply will indicate an error */
  218.     LONG rc = 20;
  219.     char* res = NULL;
  220.     char* command = msg->rm_Args[0];
  221.   /* x, y and n will be used when parsing the command */
  222.     int x, y, n;
  223.     /* Parse the command */
  224.     if(strcmp(command, "QUIT") == 0)
  225.     {
  226.         going = FALSE;
  227.         /* We recognised the command, so set rc to zero */
  228.         rc = 0;
  229.         res = "Hello Painter is quitting";
  230.     }
  231.     else if(sscanf(command, "PEN %d", &n) == 1)
  232.     {
  233.         /* n is the pen number to use */
  234.         setFgPen(drawwin, n);
  235.         rc = 0;
  236.         res = "Pen set";
  237.     }
  238.     else if(sscanf(command, "DRAW %d %d %n", &x, &y, &n) == 2)
  239.     {
  240.         /* x and y hold the coordinate */
  241.         /* n is the offset of the rest of the string */
  242.         char* text = command+n;
  243.         Move(drawwin->RPort, x, y);
  244.         Text(drawwin->RPort, text, strlen(text));
  245.         rc = 0;
  246.         res = "Text drawn";
  247.     }
  248.     replyARexxMsg(msg, rc, res);
  249.     return going;
  250. }
  251.